<?php
/**
 * Created by PHPSTROME.
 * User: FDG367
 * Date: 2018/2/2
 * Time: 13:22
 */
namespace common\models;

use Yii;
use \yii\db\ActiveRecord;

class GoodsMarkingImg extends ActiveRecord{
    const STATUS_ENABLE = 1;
    const STATUS_DISABLE = 0;

    public static function tableName(){
        return '{{%goods_marking_img}}';
    }

    //add by huangzhao 493 【商城需求】商品图片上增加图片打标 2018-02-05 start
    //根据id获取商品打标图片
    public function getGoodsMarkingImgById($id){
        $where = ['use_flg'=>self::STATUS_ENABLE,'id'=>$id];
        //获取主商品id
        $result = $this::find()->where($where)->select('img_url')->asArray()->one();
        if(!empty($result)){
            $img_url = $result['img_url'];
        }else{
            $img_url = '';
        }
        return $img_url;
    }

    //根据商品列表获取商品打标图片$goods_list array 商品列表
    public function getGoodsMarkingImgByGoodsList($goods_list){
        $where[] = 'and';
        $where[] = ['use_flg'=>self::STATUS_ENABLE];
        if(!empty($goods_list)){
            $data_id = array_filter(array_column($goods_list,'marking_img_id'));
        }
        if(!empty($data_id)){
            $where[] = ['id'=>$data_id];
        }else{
            foreach($goods_list as &$value){
                $value['marking_img_url'] = '';
            }
            return $goods_list;
        }

        $img_url_list = array();

        $result = $this::find()->where($where)->select('img_url,id')->asArray()->all();
        if(!empty($result)){
            foreach($result as $val){
                $img_url_list[$val['id']] = $val;
            }
        }

        if(!empty($img_url_list)){
            foreach($goods_list as &$value){
                if(!empty($value['marking_img_id'])){
                    $value['marking_img_url'] = empty($img_url_list[$value['marking_img_id']]['img_url']) ? '' : $img_url_list[$value['marking_img_id']]['img_url'];
                }else{
                    $value['marking_img_url'] = '';
                }
            }
        }
        return $goods_list;
    }
    //add by huangzhao 493 【商城需求】商品图片上增加图片打标 2018-02-05 end
}